package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;

/**
 * @author atguigu-mqx
 */
@RestController
@RequestMapping("api/list")
public class ListApiController {

    //  如何让java 代码来创建mapping index！
    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Autowired
    private SearchService searchService;

    //  底层 restHighLevelClient

    //    @Autowired
    //    RestHighLevelClient restHighLevelClient;

    @GetMapping("inner/createIndex")
    public Result createIndex(){
        //  方法划线了。说明过时了，能用！  如果用的是6.x ; 如果是7.x 版本，只要启动项目就会自动创建！
        this.restTemplate.createIndex(Goods.class);
        this.restTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    //  商品上架
    @GetMapping("inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId){
        //  调用服务层方法
        this.searchService.upperGoods(skuId);
        return Result.ok();
    }

    //  商品下架
    @GetMapping("inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId){
        //  调用服务层方法
        this.searchService.lowerGoods(skuId);
        return Result.ok();
    }

    //  热度排名
    @GetMapping("inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable Long skuId){
        //  调用服务层方法
        this.searchService.incrHotScore(skuId);
        return Result.ok();
    }

    //  查询数据
    @PostMapping
    public Result searchResult(@RequestBody SearchParam searchParam){

        //  调用服务层方法
        SearchResponseVo searchResponse = null;
        try {
            searchResponse = this.searchService.search(searchParam);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.ok(searchResponse);
    }

}
